package date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @auther XuGeGe
 * @date 2022/6/27
 */
public class Month {
    public static void main(String[] args) throws ParseException {
        int account = 2;
        System.out.println("总次数： " + account);
        int frequencyTime = 23;
        System.out.println("每月：" + frequencyTime);
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf1.parse("2022-02-01");
        //计算下一次访问时间
        Date startTime = parse;
        System.out.println("开始时间: " + sdf1.format(startTime));
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTime);


        //下一次随访时间
        Date nextFollowDate = new Date();
        //结束时间
        Date endDate = new Date();
        //开始时间号数
        int startDateNumber = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println("开始号数： " + startDateNumber);
        //判断状态 0表示没有选择的日期没有大于
        int status = 0;
        if (startDateNumber <= frequencyTime) {
            //如果用户选择的日期大于了本月最大天数 按最后一天
            if (frequencyTime > getMaxMonthNumber(startTime)) {
                nextFollowDate = getAddDay(startTime, getMaxMonthNumber(startTime) - startDateNumber);
            } else {
                nextFollowDate = getAddDay(startTime, frequencyTime - startDateNumber);
            }

        }
        if (startDateNumber > frequencyTime) {
            int lastNumber = getMaxMonthNumber(startTime);
            nextFollowDate = getAddDay(startTime, lastNumber - startDateNumber + frequencyTime);
        }

        System.out.println("下一次随访时间： " + sdf1.format(nextFollowDate));

        //计算结束时间
        endDate = addMonth(nextFollowDate, account - 1);
        if (frequencyTime > getMaxMonthNumber(startTime)) {
            endDate = getDateByNumber(endDate, getMaxMonthNumber(endDate));
            endDate = getAddDay(endDate, 1);
        } else {
            endDate = getAddDay(endDate, 1);
        }
        System.out.println("结束时间： " + sdf1.format(endDate));
    }

    /**
     * 根据当期日期设置当前月份号数
     */
    public static Date getDateByNumber(Date date, int number) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, number);
        return cal.getTime();
    }

    /**
     * 获取号数
     */
    public static int getNumber(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_MONTH);
        return i;
    }

    /**
     * 获取当前日期月份最大天数
     */
    public static int getMaxMonthNumber(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //获取当前日期月份最大天数
        int lastNumber = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        return lastNumber;
    }

    //当前日期 + n 天
    public static Date getAddDay(Date startTime, int dayNumber) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        calendar.add(calendar.DATE, dayNumber); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }

    //增加月份
    public static Date addMonth(Date startTime, int month) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startTime);
        calendar.add(calendar.MONTH, month); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime(); //这个时间就是日期往后推一天的结果
    }
}
